/**
 * 
 */
package test;

import org.apache.log4j.Logger;
import junit.framework.TestCase;


import engine.Brain;
import engine.Chessboard;
import engine.Move;
import engine.Noopy;

/**
 * @author Brandon
 *
 */
public class EngineTester extends TestCase{
	
	  

	final static String className = EngineTester.class.getName();
	final static Logger LOG = Logger.getLogger(className);
	
	public void testEngine(){
	
		System.out.println("Starting Test::TestEngine");
		Chessboard cb = new Chessboard();
		Noopy noop = new Noopy();
		
		cb.setupBoard();
		System.out.println("FEN " + cb.getFEN());	
		try{
			
			Move usermove = Noopy.receiveMove("e2e3", cb);
			System.out.println("Move= " + usermove.pieceMoving + ", " + usermove.fromIndex + ", " + usermove.toIndex);
			cb.makeMove(usermove);
			System.out.println("FEN " + cb.getFEN());
			Brain.LineEval bestLine = new Brain.LineEval();
			
			bestLine = Brain.search(cb); 
			cb.makeMove((Move)bestLine.line.get(0)); 
			System.out.println("move " + (((Move)bestLine.line.get(0)).inputNotation()));
			System.out.println("FEN " + cb.getFEN());	
			
		}catch(Exception e){
		System.out.println("Failed with Exception = " + e);
		e.printStackTrace();
		assertEquals("bad assertion", true, true);
		}
	
		
	}
	
	
	public void testEnPassent(){
		
		System.out.println("Starting Test::TestEnPassent");
		Chessboard cb = new Chessboard();
		Noopy noop = new Noopy();
		
		cb.setupBoard();
		cb.inputFEN("1nbqkbnr/1ppppppp/r7/p7/8/P3P3/1PPP1PPP/RNBQKBNR w KQk - 1 3");
		System.out.println("FEN after setup: " + cb.getFEN());	
		try{
			
			Move usermove = Noopy.receiveMove("f1a6", cb);
			cb.makeMove(usermove);
			System.out.println("FEN " + cb.getFEN());
			cb.unmakeMove(usermove);
			
			System.out.println("FEN after unmake " + cb.getFEN());
			//Brain.LineEval bestLine = new Brain.LineEval();
			
			
			//bestLine = Brain.search(cb); 
			//cb.makeMove((Move)bestLine.line.get(0)); 
			
			
		}catch(Exception e){
		System.out.println("Failed with Exception = " + e);
		e.printStackTrace();
		assertEquals("bad assertion", true, true);
		}
		
	}
	
	
	
	public void testKnightInCorner(){
		
		System.out.println("Starting Test::KnightInCorner");
		Chessboard cb = new Chessboard();
		Noopy noop = new Noopy();
		
		cb.setupBoard();
		cb.inputFEN("1rbqkbnr/pppppppp/2n5/8/4P3/5N2/PPPP1PPP/RNBQKB1R w KQk - 3 3");
		System.out.println("FEN after setup: " + cb.getFEN());	
		try{
			
			Move usermove = Noopy.receiveMove("d2d4", cb);
			cb.makeMove(usermove);
			System.out.println("FEN " + cb.getFEN());
			Brain.LineEval bestLine = new Brain.LineEval();
			
			
			bestLine = Brain.search(cb); 
			cb.makeMove((Move)bestLine.line.get(0)); 
			System.out.println("move " + (((Move)bestLine.line.get(0)).inputNotation()));
			System.out.println("FEN " + cb.getFEN());	
			
		}catch(Exception e){
		System.out.println("Failed with Exception = " + e);
		e.printStackTrace();
		assertEquals("bad assertion", true, true);
		}
		
	}
	
public void testCornerCapture(){
		
		System.out.println("Starting Test::TestCornerCapture");
		Chessboard cb = new Chessboard();
		Noopy noop = new Noopy();
		
		cb.setupBoard();
		cb.inputFEN("rnbqkbnr/pp2pppp/8/P1pp4/8/8/1PPPPPPP/1NBQKBNR b Kkq - 0 3");
		System.out.println("FEN after setup: " + cb.getFEN());	
		try{
			
			Move usermove = Noopy.receiveMove("e7e5", cb);
			
			System.out.println("FEN " + cb.getFEN());
			
			//usermove = Noopy.receiveMove("g1h1", cb);
			
			
			cb.makeMove(usermove);
			
			Brain.LineEval bestLine = new Brain.LineEval();
			
			
			bestLine = Brain.search(cb);
			 
			
			System.out.println("FEN " + cb.getFEN());	
			
		}catch(Exception e){
		System.out.println("Failed with Exception = " + e);
		e.printStackTrace();
		assertEquals("bad assertion", true, true);
		}
		
	}

public void testPawnOnEdge(){
	
	System.out.println("Starting Test::TestPawnOnEdge");
	Chessboard cb = new Chessboard();
	Noopy noop = new Noopy();
	
	cb.setupBoard();
	cb.inputFEN("rnbqkbnr/pp2pppp/8/P1pp4/8/8/1PPPPPPP/RNBQKBNR b KQkq - 0 1");
	System.out.println("FEN after setup: " + cb.getFEN());	
	try{
		
		Move usermove = Noopy.receiveMove("e7e5", cb);
		
		
		
		//usermove = Noopy.receiveMove("g1h1", cb);
		
		
		cb.makeMove(usermove);
		System.out.println("FEN " + cb.getFEN());
		
		Brain.LineEval bestLine = new Brain.LineEval();
		
		
		bestLine = Brain.search(cb);
		
						
		System.out.println("move " + (((Move)bestLine.line.get(0)).inputNotation()));
		 
		
		System.out.println("FEN " + cb.getFEN());	
		
	}catch(Exception e){
	System.out.println("Failed with Exception = " + e);
	e.printStackTrace();
	assertEquals("bad assertion", true, true);
	}
	
}
	
public void testBrainEval(){
		
		System.out.println("testBrainEval");
		Chessboard cb = new Chessboard();
	
		
		cb.setupBoard();
		cb.inputFEN("r1b1kbnr/pP2pppp/2n5/2pp4/8/8/1PPPPPPP/qNBQKBNR w Kkq - 1 6");
		System.out.println("FEN after setup: " + cb.getFEN());	
		try{
			
			int eval = Brain.evaluatePosition(cb);
			
			
			System.out.println("Eval =  " + eval);	
			
		}catch(Exception e){
		System.out.println("Failed with Exception = " + e);
		assertEquals("bad assertion", true, true);
		}
		
	}

	public void testGenMoves(){
	
	System.out.println("testBrainEval");
	Chessboard cb = new Chessboard();

	
	cb.setupBoard();
	cb.inputFEN("r1b1kbnr/pP2pppp/2n5/2pp4/8/8/1PPPPPPP/qNBQKBNR w Kkq - 1 6");
	System.out.println("FEN after setup: " + cb.getFEN());	
	try{
		
		cb.generateMoves(false);
		
		
		System.out.println("FEN after test: " + cb.getFEN());
		
	}catch(Exception e){
	System.out.println("Failed with Exception = " + e);
	assertEquals("bad assertion", true, true);
	
	// bad line:  r1bqk2r/ppppppbp/7p/n2P4/1P1NP3/P1N4P/2P1BPP1/R2Q1RK1 w kq - 0 14
	}
	
}
	
public void testCheckMate(){
		
		System.out.println("Starting Test::CheckMate");
		Chessboard cb = new Chessboard();
		Noopy noop = new Noopy();
		
		cb.setupBoard();
		cb.inputFEN("6Q1/2pk2pp/B1p2p2/2B1p3/8/5PP1/2N4P/4K1NR w K - 0 36");
		System.out.println("FEN after setup: " + cb.getFEN());	
		try{
			
			Move usermove = Noopy.receiveMove("g8c8", cb);
			cb.makeMove(usermove);
			System.out.println("FEN " + cb.getFEN());
			Brain.LineEval bestLine = new Brain.LineEval();
			
			
			bestLine = Brain.search(cb); 
			if(bestLine.line.size() == 0)
				System.out.println("Checkmate -- no moves returned!");
			
			System.out.println("FEN " + cb.getFEN());	
			
		}catch(Exception e){
		System.out.println("Failed with Exception = " + e);
		e.printStackTrace();
		assertEquals("bad assertion", true, true);
		}
		
	}
}
